﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using Peter;

namespace NetCommander
{
    public partial class FileLister : UserControl
    {
        public FileLister()
        {
            InitializeComponent();
        }

        public DirectoryInfo CurrentPath { get; set; }
        private FileSystemWatcher watcher = new FileSystemWatcher();
        

        private void FileLister_Load(object sender, EventArgs e)
        {
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                cbPath.Items.Add(new DriveListItem(drive));
            }

            CurrentPath = new DirectoryInfo(Directory.GetCurrentDirectory());
            RefreshContent();
            watcher.Changed += new FileSystemEventHandler(watcherEvent);
            watcher.Created += new FileSystemEventHandler(watcherEvent);
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents = true;

        }

        void watcherEvent(object sender, FileSystemEventArgs e)
        {
            
        }



        private void cbPath_SelectedIndexChanged(object sender, EventArgs e)
        {
            //CurrentPath = new DirectoryInfo(cbPath.SelectedValue.ToString());
            //RefreshContent();
        }

        public void RefreshContent()
        {
            fileList.Items.Clear();

            if (CurrentPath.Root.Name != CurrentPath.Name)
            {
                ListViewItem upDir = new ListViewItem(new string[] { "..", "", "<UP-DIR>", "" });
                upDir.Tag = "Updir";
                fileList.Items.Add(upDir);
            }

            foreach (DirectoryInfo dir in CurrentPath.GetDirectories())
            {
                ListViewItem dirItem = new ListViewItem((new string[] { dir.Name, "","<SUB-DIR>", dir.LastWriteTime.ToString() }));
                dirItem.Tag = "Subdir";
                fileList.Items.Add(dirItem);
            }

            foreach (FileInfo file in CurrentPath.GetFiles())
            {
                ListViewItem fileItem = new ListViewItem((new string[] { file.Name, file.Extension, file.Length.ToString(), file.LastWriteTime.ToString() }));
                fileItem.Tag = "File";
                fileList.Items.Add(fileItem);
            }
            cbPath.Text = CurrentPath.FullName;
            fileList.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);

            watcher.Path = CurrentPath.FullName;
        }

        private void fileList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (fileList.SelectedItems.Count == 1)
            {
                ListViewItem item = fileList.SelectedItems[0];
                switch ((string)item.Tag)
                {
                    case "Updir":
                        {
                            CurrentPath = CurrentPath.Parent;
                            RefreshContent();
                            break;
                        }
                    case "Subdir":
                        {
                            CurrentPath = new DirectoryInfo(Path.Combine(CurrentPath.FullName, item.Text));
                            RefreshContent();
                            break;
                        }
                    case "File":
                        {
                            try
                            {
                                Process.Start(Path.Combine(CurrentPath.FullName, item.Text));
                            }
                            catch (Win32Exception)
                            {
                                MessageBox.Show("No program is associated with the selected file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
                            }
                            catch (Exception exception)
                            {
                            }
                            break;
                        }
                }
            }
        }

        public void CopyTo(DirectoryInfo destination)
        {
            foreach (ListViewItem item in fileList.SelectedItems)
            {
                FileInfo file = new FileInfo(Path.Combine(CurrentPath.FullName, item.Text));
                file.CopyTo(Path.Combine(destination.FullName, item.Text), true);
            }
        }

        private void fileList_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void fileList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Insert)
            {
                
            }
        }

        private void fileList_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                FileInfo file = new FileInfo(Path.Combine(CurrentPath.FullName, fileList.SelectedItems[0].Text));
                ShellContextMenu shellMenu = new ShellContextMenu();
                shellMenu.ShowContextMenu(new FileInfo[]{file}, this.PointToScreen(e.Location));

            }
        }

    }
}
